var express = require('express');
var router = express.Router();
let { home, area } = require('../model/model');

/* GET home page. */
// 添加地区
router.post('/addarea', async (req, res) => {
    area.create(req.body)
    res.send({
        code: 200,
        msg: '地区添加完成'
    })
})

// 添加地区房源信息
router.post('/addhouse', async (req, res) => {
    home.create(req.body)
    res.send({
        code: '200',
        msg: '房子添加成功'
    })
})

router.get('/gethome', async (req, res) => {
    // 获取热门目的地房源
    let homelist = await home.find().populate('u_id')
    // 获取热门目的地
    let addresslist = await area.find()
    // 故事房间+分页
    let { page, pagesize } = req.query
    let pagehouse = await home.find().populate('u_id').skip(Number((page - 1) * pagesize)).limit(Number(pagesize))
    let totalpage = await home.find().countDocuments()

    res.send({
        code: 200,
        homelist,
        addresslist,
        pagehouse,
        totalpage
    })
})


// 修改收藏状态

router.post('/editHouse',async(req,res)=>{
    let body=req.body
    console.log(body,'body');
    
    let id=body.id
    console.log(id,'idid');
    
    await home.updateOne({_id:id},{collect:body.collect})
    res.send({
        code:200
    })
})











module.exports = router;